import {
  signInPwd,
  logout,
  getInfo
} from '@/api/user'
import {
  getMenu
} from '@/api/role/index'

import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    menus: [],
    buttonList: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_MENUS: (state, menus) => {
    state.menus = menus
  },
  SET_BUTTONLIST: (state, buttonList) => {
    state.buttonList = buttonList
  }


}

const actions = {
  // user login
  login({
    commit
  }, userInfo) {
    return new Promise((resolve, reject) => {
      signInPwd(userInfo).then(res => {
        if (res.code == 200) {
          let data = res.data
          commit('SET_TOKEN', data.token)
          setToken(data.token)
          resolve(data)
          // console.log(data)
        }
      }).catch(error => {
        reject(error)
        console.log(error)
      })
    })
  },

  getrole({
    commit
  }, userInfo) {
    return new Promise((resolve, reject) => {
      getMenu().then(res => {
        if (res.code == 200) {
          const {
            data
          } = res
          commit('SET_MENUS', data)
          resolve(data)
          // console.log(data)
        }
      }).catch(error => {
        reject(error)
        console.log(error)
      })
    })
  },

  // get user info
  getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      // getInfo(state.token).then(response => {
      const response = {
        data: {
          role_name: 'tox',
          permissions: ['system:users:add']
        }
      }
      const {
        data
      } = response
      console.log(data)
      if (!data) {
        return reject('Verification failed, please Login again.')
      }
      const {
        role_name,
        permissions
      } = data
      commit('SET_NAME', role_name)
      commit('SET_BUTTONLIST', permissions)
      // 动态添加404
      let menus = [{
          "path": '/customer',
          "component": "Layout",
          "redirect": '/customer/customer',
          "meta": {
            "title": '客户管理',
            "icon": 'el-icon-s-custom'
          },
          "children": [{
            "path": 'customer',
            "name": 'customer',
            "component": 'customer/index',
            "meta": {
              "title": '客户列表',
              "icon": ''
            }
          }, ],
        },
        {
          "path": '/staff',
          "component": "Layout",
          "redirect": '/staff/staff',
          "meta": {
            "title": '员工管理',
            "icon": 'el-icon-user-solid'
          },
          "children": [{
            "path": 'staff',
            "name": 'staff',
            "component": 'staff/index',
            "meta": {
              "title": '员工列表',
              "icon": ''
            }
          }, ],
        },
        {
          "path": '/user',
          "component": "Layout",
          "redirect": '/user/user',
          "meta": {
            "title": '权限管理',
            "icon": 'el-icon-user-solid'
          },
          "children": [{
              "path": 'user',
              "name": 'user',
              "component": 'user/index',
              "meta": {
                "title": '用户管理',
                "icon": ''
              }
            },
            {
              "path": 'menu',
              "name": 'menu',
              "component": 'menu/index',
              "meta": {
                "title": '菜单管理',
                "icon": ''
              }
            },
            {
              "path": 'role',
              "name": 'role',
              "component": 'role/index',
              "meta": {
                "title": '角色管理',
                "icon": ''
              }
            }

          ]
        },
        {
          "path": '/product',
          "component": "Layout",
          "redirect": '/product/product',
          "meta": {
            "title": '产品管理',
            "icon": 'el-icon-s-tools'
          },
          "children": [{
              "path": 'product',
              "name": 'product',
              "component": 'product/index',
              "meta": {
                "title": '产品列表',
                "icon": ''
              }
            },
            {
              "path": 'colorlist',
              "name": 'colorlist',
              "component": 'color/index',
              "meta": {
                "title": '颜色管理',
                "icon": ''
              }
            },
            {
              "path": 'jobtype',
              "name": 'jobtype',
              "component": 'jobtype/index',
              "meta": {
                "title": '工种管理',
                "icon": ''
              }
            },
            {
              "path": 'modelnumber',
              "name": 'modelnumber',
              "component": 'modelnumber/index',
              "meta": {
                "title": '型号管理',
                "icon": ''
              }
            },
            {
              "path": 'specification',
              "name": 'specification',
              "component": "specification/index",
              "meta": {
                "title": '规格管理',
                "icon": ''
              }
            },
          ],
        }
      ]
      // commit('SET_MENUS', menus)
      // commit('SET_AVATAR', data.avatar)
      resolve(data)
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // user logout
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        commit('SET_ROLES', [])
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
